package com.dycong.common.IO.netty.frame_coder;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * Created by dycong on 2017/4/17.
 */
public class MyMessageToByteEncoder extends MessageToByteEncoder<Msg> {

    @Override
    public void encode(ChannelHandlerContext ctx, Msg msg, ByteBuf out) throws Exception{
        if(null == msg){
            throw new Exception("msg is null");
        }
        out.writeByte(msg.getType());
        out.writeByte(msg.getStatus());
        out.writeInt(msg.getLength());
        out.writeBytes(msg.getBody().getBytes());
    }

}
